<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <title>修改我的账户</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            font-family: Arial, sans-serif;
            background-color: #f8f9fa;
            color: #333;
            line-height: 1.6;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 0 15px;
        }
        .profile-container {
            max-width: 800px;
            margin: 50px auto;
            padding: 30px;
            background-color: #fff;
            border-radius: 10px;
            box-shadow: 0 0 20px rgba(0,0,0,0.1);
        }
        .profile-header {
            text-align: center;
            margin-bottom: 30px;
        }
        .profile-header h2 {
            color: #333;
            font-size: 24px;
            margin-bottom: 10px;
        }
        .form-group {
            margin-bottom: 20px;
        }
        .form-label {
            display: block;
            margin-bottom: 8px;
            font-weight: 500;
            color: #333;
        }
        .form-control {
            width: 100%;
            padding: 10px 15px;
            font-size: 16px;
            border: 1px solid #ddd;
            border-radius: 4px;
            transition: border-color 0.3s;
        }
        .form-control:focus {
            border-color: #007bff;
            outline: none;
        }
        .btn {
            display: inline-block;
            padding: 10px 20px;
            font-size: 16px;
            font-weight: 500;
            text-align: center;
            text-decoration: none;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        .btn-primary {
            background-color: #007bff;
            color: #fff;
            border: none;
        }
        .btn-primary:hover {
            background-color: #0056b3;
        }
        .text-center {
            text-align: center;
        }
        .form-text {
            font-size: 0.875em;
            color: #6c757d;
            margin-top: 0.25rem;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="profile-container">
        <div class="profile-header">
            <h2>修改我的账户</h2>
        </div>

        <form id="updateForm">
            <div class="form-group">
                <label for="account" class="form-label">账号</label>
                <input type="text" class="form-control" id="account" name="account" value="${LoginUser.account}">
                <small id="accountHelp" class="form-text text-muted"></small>
            </div>

            <div class="form-group">
                <label for="password" class="form-label">新密码</label>
                <input type="password" class="form-control" id="password" name="password" placeholder="不修改请留空">
            </div>

            <div class="form-group">
                <label for="repassword" class="form-label">确认新密码</label>
                <input type="password" class="form-control" id="repassword" name="repassword" placeholder="不修改请留空">
            </div>

            <div class="text-center">
                <button type="submit" class="btn btn-primary">保存修改</button>
            </div>
        </form>
    </div>
</div>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
    $(document).ready(function() {
        // 提交表单
        $('#updateForm').submit(function(e) {
            e.preventDefault();

            var account = $('#account').val();
            if (account.length < 2 || account.length > 10) {
                $('#accountHelp').text('账号长度必须在 2 到 10 位之间').css('color', 'red');
                return;
            } else {
                $('#accountHelp').text('');
            }

            var password = $('#password').val();
            var repassword = $('#repassword').val();
            if (password !== repassword) {
                alert('两次输入的密码不一致！');
                return;
            }

            var formData = $(this).serialize();

            $.ajax({
                url: '/admin/profile/update',
                type: 'POST',
                data: formData,
                success: function(response) {
                    if (response === 'success') {
                        alert('更新成功！');
                        window.location.reload(); // 刷新当前页面
                    } else if (response.startsWith("redirect:")) {
                        alert('密码修改成功，请重新登录！');
                        window.location.href = response.substring("redirect:".length);
                    } else {
                        alert(response);
                    }
                },
                error: function() {
                    alert('系统错误，请稍后再试');
                }
            });
        });
    });
</script>
</body>
</html>